package com.saudidentalapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Responce;
import com.saudiapp.webservice.Utilities;
import com.saudiapp.webservice.WebServiceManager;
import com.saudiapp.webservice.WebServiceManager.WebServiceListener;

public class FacebookActivity extends Activity implements WebServiceListener
{
	TextView txtTopHeading;
	Utilities utilities;
	WebView webView;
	final Activity activity = this;
	 @Override
	protected void onCreate(Bundle savedInstanceState) 
	 {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.activity_facebook_twitter);
		txtTopHeading=(TextView)findViewById(R.id.xtxtTopHeading);		
		txtTopHeading.setText("Facebook");
		prepareBrowserView();
		setView();
	}
	 
	private void setView()
	{
		utilities=Utilities.getInstance(FacebookActivity.this);
		utilities.showProgreessDialog();
		if(utilities.haveNetworkConnection())
		{
			WebServiceManager webServiceManager=new WebServiceManager(this);
			webServiceManager.setonWebServiceListener(this);
			webServiceManager.getFacebookUrl();	
		}
		else
		{
		utilities.dismiss();
		utilities.showToast(Constants.INTERNET_NOT_AVAILABELE);
		}
		
		
		final Activity activity=this;
		ImageView imgBack=(ImageView)findViewById(R.id.xbtnVwBack);
		imgBack.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				finish();
				startActivity(new Intent(activity,TabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				
			}
		});
	}
	
	void prepareBrowserView()
	{
		webView=(WebView)findViewById(R.id.webView);	
		// getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
		 webView.getSettings().setJavaScriptEnabled(true);  
		 webView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this
	                                              //if ROM supports Multi-Touch     
		 webView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
	      
		 
		 webView.setWebChromeClient(new WebChromeClient() 
		 {
		        public void onProgressChanged(WebView view, int progress)  
		        {
		         //Make the bar disappear after URL is loaded, and changes string to Loading...
		        	//activity.setTitle("Loading...");
		        	//activity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
		 
		         // Return the app name after finish loading
		            if(progress == 100)
		            	utilities.dismiss();	
		          }
		        
		        
		        });
		 webView.setWebViewClient(new WebViewClient() {
	            @Override
	            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
	            {
	            	utilities.dismiss();	
	            }
	  
	            @Override
	            public boolean shouldOverrideUrlLoading(WebView view, String url)
	            {
	                view.loadUrl(url);
	                return true;
	            }
	        });
	}

	@Override
	public void onProcesCompleted(int requestId, Object Data) 
	{	
		//utilities.dismiss();
		webView.loadUrl(((Responce)Data).getResponceData().toString());
	}

	@Override
	public void onErrorOccured(int requestId) 
	{
		utilities.dismiss();	
		utilities.showToast(Constants.ERROR_MESSAGE);
		
	}
}